package liuwanghui.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import liuwanghui.dto.request.ProductCreateDTO;
import liuwanghui.dto.request.ProductQueryDTO;
import liuwanghui.dto.request.ProductStatusDTO;
import liuwanghui.dto.request.ProductUpdateDTO;
import liuwanghui.pojo.Result;
import liuwanghui.service.FarmerService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/farmer")
@RequiredArgsConstructor
@CrossOrigin
@Tag(name = "农户", description = "农户相关接口")
public class FarmerController {

    private final FarmerService farmerService;

    @GetMapping("/products")
    @Operation(summary = "获取商品列表")
    public Result getProducts(ProductQueryDTO params) {
        return farmerService.getProducts(params);
    }

    @PostMapping("/products")
    @Operation(summary = "创建商品")
    public Result createProduct(@RequestBody @Valid ProductCreateDTO dto) {
        return farmerService.createProduct(dto);
    }

    @PutMapping("/products/{id}")
    @Operation(summary = "更新商品")
    public Result updateProduct(@PathVariable Long id, @RequestBody @Valid ProductUpdateDTO dto) {
        return farmerService.updateProduct(id, dto);
    }

    @DeleteMapping("/products/{id}")
    @Operation(summary = "删除商品")
    public Result deleteProduct(@PathVariable Long id) {
        farmerService.deleteProduct(id);
        return Result.success();
    }

    @PutMapping("/products/{id}/status")
    @Operation(summary = "更新商品状态")
    public Result updateProductStatus(@PathVariable Long id, @RequestBody @Valid ProductStatusDTO dto) {
        farmerService.updateProductStatus(id, dto);
        return Result.success();
    }

    @GetMapping("/dashboard/stats")
    @Operation(summary = "获取仪表盘统计")
    public Result getDashboardStats(
            @RequestParam(required = false) String startDate,
            @RequestParam(required = false) String endDate) {
        return farmerService.getDashboardStats(startDate, endDate);
    }
}